﻿'//DotNetNuke - http://www.dotnetnuke.com
'// Copyright (c) 2002-2010
'// by DotNetNuke Corporation
'//
'// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
'// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
'// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
'// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'//
'// The above copyright notice and this permission notice shall be included in all copies or substantial portions
'// of the Software.
'//
'// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
'// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
'// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
'// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
'// DEALINGS IN THE SOFTWARE.


Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Entities.Content
Imports DotNetNuke.Entities.Content.Common
Imports System.Linq

Namespace Components.Taxonomy 

    Public Class Content

        Private ContentTypeName As String = "SimpleArticle"

        Public Function CreateContentItem(ByVal objArticle As Article, ByVal tabId As Integer)
            Dim typeController As New ContentTypeController
            Dim colContentTypes = (From t As ContentType In typeController.GetContentTypes() Where t.ContentType = ContentTypeName Select t)
            Dim contentTypeId As Integer
            If colContentTypes.Count > 0 Then
                Dim contentType As ContentType = colContentTypes.Single()
                If Null.IsNull(contentType) Then
                    contentTypeId = CreateContentType()
                Else
                    contentTypeId = contentType.ContentTypeId
                End If

            Else
                contentTypeId = CreateContentType()
            End If

            Dim objContent As New ContentItem()
            objContent.Content = objArticle.Body
            objContent.ContentTypeId = contentTypeId
            objContent.Indexed = False
            objContent.ContentKey = "aid=" & objArticle.ArticleId
            objContent.TabID = tabId
            objContent.ModuleID = objArticle.ModuleId
            objContent.ContentItemId = Util.GetContentController().AddContentItem(objContent)

            'add terms
            Dim cntTerm As New Terms
            cntTerm.ManageArticleTerms(objArticle, objContent)
            Return objContent
        End Function

        Public Sub UpdateContentItem(ByVal objArticle As Article, ByVal tabId As Integer)
            Dim objContent = Util.GetContentController.GetContentItem(objArticle.ContentItemId)
            If Null.IsNull(objContent) Then
                Return
            End If
            objContent.Content = objArticle.Body
            objContent.TabID = tabId
            Util.GetContentController.UpdateContentItem(objContent)

            'update terms
            Dim cntTerm As New Terms
            cntTerm.ManageArticleTerms(objArticle, objContent)
        End Sub


        Public Sub DeleteContentItem(ByVal objArticle As Article)
            If objArticle.ContentItemId <= Null.NullInteger Then
                Return
            End If
            Dim objContent = Util.GetContentController.GetContentItem(objArticle.ContentItemId)
            If Null.IsNull(objContent) Then
                Return
            End If
            Dim cntTerm As New Terms
            cntTerm.RemoveArticleTerms(objArticle)
            Util.GetContentController.DeleteContentItem(objContent)
        End Sub




        Private Function CreateContentType() As Integer
            Dim typeController As New ContentTypeController
            Dim objContentType As New ContentType
            objContentType.ContentType = ContentTypeName
            Return typeController.AddContentType(objContentType)

        End Function

    End Class

End Namespace
